/*     */ package com.zimbra.cs.service.formatter;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.HttpUtil;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.DeliveryOptions;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mailbox.calendar.CalendarMailSender;
/*     */ import com.zimbra.cs.mailbox.calendar.Invite;
/*     */ import com.zimbra.cs.mailbox.util.TagUtil;
/*     */ import com.zimbra.cs.mime.Mime;
/*     */ import com.zimbra.cs.mime.ParsedMessage;
/*     */ import com.zimbra.cs.service.UserServletContext;
/*     */ import com.zimbra.cs.service.UserServletException;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.EnumSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import javax.mail.internet.MimePart;
/*     */ import javax.servlet.ServletOutputStream;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class SyncFormatter
/*     */   extends Formatter
/*     */ {
/*     */   public static final String QP_NOHDR = "nohdr";
/*     */   
/*     */   public FormatterFactory.FormatType getType()
/*     */   {
/*  61 */     return FormatterFactory.FormatType.SYNC;
/*     */   }
/*     */   
/*     */ 
/*     */   public Set<MailItem.Type> getDefaultSearchTypes()
/*     */   {
/*  67 */     return EnumSet.of(MailItem.Type.MESSAGE);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private static List<Pair<String, String>> getXZimbraHeaders(MailItem item)
/*     */   {
/*  74 */     List<Pair<String, String>> hdrs = new ArrayList();
/*  75 */     hdrs.add(new Pair("X-Zimbra-ItemId", item.getId() + ""));
/*  76 */     hdrs.add(new Pair("X-Zimbra-FolderId", item.getFolderId() + ""));
/*  77 */     hdrs.add(new Pair("X-Zimbra-Tags", TagUtil.getTagIdString(item)));
/*  78 */     hdrs.add(new Pair("X-Zimbra-Tag-Names", TagUtil.encodeTags(item.getTags())));
/*  79 */     hdrs.add(new Pair("X-Zimbra-Flags", item.getFlagString()));
/*  80 */     hdrs.add(new Pair("X-Zimbra-Received", item.getDate() + ""));
/*  81 */     hdrs.add(new Pair("X-Zimbra-Modified", item.getChangeDate() + ""));
/*  82 */     hdrs.add(new Pair("X-Zimbra-Change", item.getModifiedSequence() + ""));
/*  83 */     hdrs.add(new Pair("X-Zimbra-Revision", item.getSavedSequence() + ""));
/*  84 */     if ((item instanceof Message))
/*  85 */       hdrs.add(new Pair("X-Zimbra-Conv", ((Message)item).getConversationId() + ""));
/*  86 */     return hdrs;
/*     */   }
/*     */   
/*     */   private static byte[] getXZimbraHeadersBytes(List<Pair<String, String>> hdrs) {
/*  90 */     StringBuilder sb = new StringBuilder();
/*  91 */     for (Pair<String, String> pair : hdrs)
/*  92 */       sb.append((String)pair.getFirst()).append(": ").append((String)pair.getSecond()).append("\r\n");
/*  93 */     return sb.toString().getBytes();
/*     */   }
/*     */   
/*     */   public static byte[] getXZimbraHeadersBytes(MailItem item) {
/*  97 */     return getXZimbraHeadersBytes(getXZimbraHeaders(item));
/*     */   }
/*     */   
/*     */   private static void addXZimbraHeaders(UserServletContext context, MailItem item, long size) throws IOException {
/* 101 */     List<Pair<String, String>> hdrs = getXZimbraHeaders(item);
/* 102 */     for (Pair<String, String> pair : hdrs) {
/* 103 */       context.resp.addHeader((String)pair.getFirst(), (String)pair.getSecond());
/*     */     }
/*     */     
/*     */ 
/* 107 */     if (context.params.get("nohdr") == null) {
/* 108 */       byte[] inline = getXZimbraHeadersBytes(hdrs);
/* 109 */       if (size > 0L)
/* 110 */         context.resp.setContentLength(inline.length + (int)size);
/* 111 */       context.resp.getOutputStream().write(inline);
/* 112 */     } else if (size > 0L) {
/* 113 */       context.resp.setContentLength((int)size);
/*     */     }
/*     */   }
/*     */   
/*     */   public void formatCallback(UserServletContext context) throws IOException, ServiceException, UserServletException
/*     */   {
/*     */     try {
/* 120 */       if (context.hasPart()) {
/* 121 */         handleMessagePart(context, context.target);
/* 122 */       } else if ((context.target instanceof Message)) {
/* 123 */         handleMessage(context, (Message)context.target);
/* 124 */       } else if ((context.target instanceof CalendarItem))
/*     */       {
/* 126 */         CalendarItem calItem = (CalendarItem)context.target;
/* 127 */         if ((calItem.isPublic()) || (calItem.allowPrivateAccess(context.getAuthAccount(), context.isUsingAdminPrivileges())))
/*     */         {
/* 129 */           handleCalendarItem(context, calItem);
/*     */         } else {
/* 131 */           context.resp.sendError(403, "permission denied");
/*     */         }
/*     */       }
/*     */     } catch (MessagingException me) {
/* 135 */       throw ServiceException.FAILURE(me.getMessage(), me);
/*     */     }
/*     */   }
/*     */   
/*     */   private void handleCalendarItem(UserServletContext context, CalendarItem calItem) throws IOException, ServiceException, MessagingException {
/* 140 */     context.resp.setContentType("text/plain");
/* 141 */     if (context.itemId.hasSubpart()) {
/* 142 */       Pair<MimeMessage, Integer> calItemMsgData = calItem.getSubpartMessageData(context.itemId.getSubpartId());
/* 143 */       if (calItemMsgData != null) {
/* 144 */         addXZimbraHeaders(context, calItem, -1L);
/* 145 */         ((MimeMessage)calItemMsgData.getFirst()).writeTo(context.resp.getOutputStream());
/*     */       }
/*     */       else
/*     */       {
/* 149 */         int invId = context.itemId.getSubpartId();
/* 150 */         Invite[] invs = calItem.getInvites(invId);
/* 151 */         if ((invs != null) && (invs.length > 0)) {
/* 152 */           Invite invite = invs[0];
/* 153 */           MimeMessage mm = CalendarMailSender.createCalendarMessage(invite);
/* 154 */           if (mm != null)
/*     */           {
/* 156 */             int sizeHint = mm.getSize();
/* 157 */             if (sizeHint < 0) sizeHint = 0;
/* 158 */             ByteArrayOutputStream baos = new ByteArrayOutputStream(sizeHint);
/* 159 */             mm.writeTo(baos);
/* 160 */             byte[] bytes = baos.toByteArray();
/* 161 */             addXZimbraHeaders(context, calItem, bytes.length);
/* 162 */             ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
/* 163 */             ByteUtil.copy(bais, true, context.resp.getOutputStream(), false);
/*     */           }
/*     */         }
/*     */       }
/*     */     } else {
/* 168 */       addXZimbraHeaders(context, calItem, -1L);
/* 169 */       InputStream is = calItem.getRawMessage();
/* 170 */       if (is != null)
/* 171 */         ByteUtil.copy(is, true, context.resp.getOutputStream(), false);
/*     */     }
/*     */   }
/*     */   
/*     */   private void handleMessage(UserServletContext context, Message msg) throws IOException, ServiceException {
/* 176 */     context.resp.setContentType("text/plain");
/* 177 */     InputStream is = msg.getContentStream();
/* 178 */     long size = msg.getSize();
/*     */     
/* 180 */     if (!context.shouldReturnBody()) {
/* 181 */       byte[] headers = HeadersOnlyInputStream.getHeaders(is);
/* 182 */       is = new ByteArrayInputStream(headers);
/* 183 */       size = headers.length;
/*     */     }
/* 185 */     addXZimbraHeaders(context, msg, size);
/* 186 */     ByteUtil.copy(is, true, context.resp.getOutputStream(), false);
/*     */   }
/*     */   
/*     */   private void handleMessagePart(UserServletContext context, MailItem item) throws IOException, ServiceException, MessagingException, UserServletException {
/* 190 */     if (!(item instanceof Message))
/* 191 */       throw UserServletException.notImplemented("can only handle messages");
/* 192 */     Message message = (Message)item;
/*     */     
/* 194 */     MimePart mp = getMimePart(message, context.getPart());
/* 195 */     if (mp != null) {
/* 196 */       String contentType = mp.getContentType();
/* 197 */       if (contentType == null)
/* 198 */         contentType = "application/octet-stream";
/* 199 */       sendbackOriginalDoc(mp, contentType, context.req, context.resp);
/* 200 */       return;
/*     */     }
/* 202 */     context.resp.sendError(400, "part not found");
/*     */   }
/*     */   
/*     */   public static MimePart getMimePart(CalendarItem calItem, String part) throws IOException, MessagingException, ServiceException {
/* 206 */     return Mime.getMimePart(calItem.getMimeMessage(), part);
/*     */   }
/*     */   
/*     */   public static MimePart getMimePart(Message msg, String part) throws IOException, MessagingException, ServiceException {
/* 210 */     return Mime.getMimePart(msg.getMimeMessage(), part);
/*     */   }
/*     */   
/*     */   public static void sendbackOriginalDoc(MimePart mp, String contentType, HttpServletRequest req, HttpServletResponse resp) throws IOException, MessagingException {
/* 214 */     String filename = Mime.getFilename(mp);
/* 215 */     if (filename == null)
/* 216 */       filename = "unknown";
/* 217 */     String cd = HttpUtil.createContentDisposition(req, "inline", filename);
/* 218 */     resp.addHeader("Content-Disposition", cd);
/* 219 */     String desc = mp.getDescription();
/* 220 */     if (desc != null)
/* 221 */       resp.addHeader("Content-Description", desc);
/* 222 */     sendbackOriginalDoc(mp.getInputStream(), contentType, resp);
/*     */   }
/*     */   
/*     */   public static void sendbackOriginalDoc(InputStream is, String contentType, HttpServletResponse resp) throws IOException {
/* 226 */     resp.setContentType(contentType);
/* 227 */     ByteUtil.copy(is, true, resp.getOutputStream(), false);
/*     */   }
/*     */   
/*     */   public boolean supportsSave()
/*     */   {
/* 232 */     return true;
/*     */   }
/*     */   
/*     */ 
/*     */   public void saveCallback(UserServletContext context, String contentType, Folder folder, String filename)
/*     */     throws IOException, ServiceException, UserServletException
/*     */   {
/* 239 */     byte[] body = context.getPostBody();
/*     */     try {
/* 241 */       Mailbox mbox = folder.getMailbox();
/* 242 */       ParsedMessage pm = new ParsedMessage(body, mbox.attachmentsIndexingEnabled());
/* 243 */       DeliveryOptions dopt = new DeliveryOptions().setFolderId(folder).setNoICal(true);
/* 244 */       mbox.addMessage(context.opContext, pm, dopt, null);
/*     */     } catch (ServiceException e) {
/* 246 */       throw new UserServletException(400, "error parsing message");
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/formatter/SyncFormatter.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */